/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import fr.cggveritas.crewintranet.jms.entities.CvSalaire;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvTravail;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvSalaireJpaController implements Serializable {

    public CvSalaireJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CvSalaire cvSalaire) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        CvTravail cvTravailOrphanCheck = cvSalaire.getCvTravail();
        if (cvTravailOrphanCheck != null) {
            CvSalaire oldCvSalaireOfCvTravail = cvTravailOrphanCheck.getCvSalaire();
            if (oldCvSalaireOfCvTravail != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The CvTravail " + cvTravailOrphanCheck + " already has an item of type CvSalaire whose cvTravail column cannot be null. Please make another selection for the cvTravail field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTravail cvTravail = cvSalaire.getCvTravail();
            if (cvTravail != null) {
                cvTravail = em.getReference(cvTravail.getClass(), cvTravail.getCvTravailleurId());
                cvSalaire.setCvTravail(cvTravail);
            }
            em.persist(cvSalaire);
            if (cvTravail != null) {
                cvTravail.setCvSalaire(cvSalaire);
                cvTravail = em.merge(cvTravail);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvSalaire(cvSalaire.getCvTravailleurId()) != null) {
                throw new PreexistingEntityException("CvSalaire " + cvSalaire + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvSalaire cvSalaire) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvSalaire persistentCvSalaire = em.find(CvSalaire.class, cvSalaire.getCvTravailleurId());
            CvTravail cvTravailOld = persistentCvSalaire.getCvTravail();
            CvTravail cvTravailNew = cvSalaire.getCvTravail();
            List<String> illegalOrphanMessages = null;
            if (cvTravailNew != null && !cvTravailNew.equals(cvTravailOld)) {
                CvSalaire oldCvSalaireOfCvTravail = cvTravailNew.getCvSalaire();
                if (oldCvSalaireOfCvTravail != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The CvTravail " + cvTravailNew + " already has an item of type CvSalaire whose cvTravail column cannot be null. Please make another selection for the cvTravail field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (cvTravailNew != null) {
                cvTravailNew = em.getReference(cvTravailNew.getClass(), cvTravailNew.getCvTravailleurId());
                cvSalaire.setCvTravail(cvTravailNew);
            }
            cvSalaire = em.merge(cvSalaire);
            if (cvTravailOld != null && !cvTravailOld.equals(cvTravailNew)) {
                cvTravailOld.setCvSalaire(null);
                cvTravailOld = em.merge(cvTravailOld);
            }
            if (cvTravailNew != null && !cvTravailNew.equals(cvTravailOld)) {
                cvTravailNew.setCvSalaire(cvSalaire);
                cvTravailNew = em.merge(cvTravailNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvSalaire.getCvTravailleurId();
                if (findCvSalaire(id) == null) {
                    throw new NonexistentEntityException("The cvSalaire with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvSalaire cvSalaire;
            try {
                cvSalaire = em.getReference(CvSalaire.class, id);
                cvSalaire.getCvTravailleurId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvSalaire with id " + id + " no longer exists.", enfe);
            }
            CvTravail cvTravail = cvSalaire.getCvTravail();
            if (cvTravail != null) {
                cvTravail.setCvSalaire(null);
                cvTravail = em.merge(cvTravail);
            }
            em.remove(cvSalaire);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvSalaire> findCvSalaireEntities() {
        return findCvSalaireEntities(true, -1, -1);
    }

    public List<CvSalaire> findCvSalaireEntities(int maxResults, int firstResult) {
        return findCvSalaireEntities(false, maxResults, firstResult);
    }

    private List<CvSalaire> findCvSalaireEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvSalaire.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvSalaire findCvSalaire(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvSalaire.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvSalaireCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvSalaire> rt = cq.from(CvSalaire.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
